/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin;

import Utils.Static;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import models.ContentModel;

/**
 *
 * @author user
 */
public class content extends HttpServlet {

    boolean isPost = false;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            //check if is logged in
            HttpSession session = request.getSession(true);
            //check if is connect
            if (session.getAttribute(Static.adminID) == null) {
                getServletContext().getRequestDispatcher("/admin/login/index.jsp").forward(request, response);
            }

            String script = "";
            script = "<script type=\"text/javascript\"	src=\"jscripts/tiny_mce/tiny_mce.js\"></script>";
            script += "\n";
            script += "<script type=\"text/javascript\"	src=\"jscripts/tiny_mce/tiny_mce_init.js\"></script>";
            session.setAttribute(Static.java_script, script);

            
            String action = request.getParameter(Static.action);

            if (action == null) {
                Index(request, response);
            } else if (action.compareTo(Static.edit) == 0) {
                Edit(request, response);
            } else if (action.compareTo(Static.show) == 0) {
                Show(request, response);
            }

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        isPost = false;
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        isPost = true;
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    public void Edit(HttpServletRequest request, HttpServletResponse response) {
        try {
            HttpSession session = request.getSession(true);

            int contentID = Integer.parseInt(request.getParameter(Static.id));
            String content = request.getParameter("txtContent");

            ContentModel contentModel = new ContentModel();

            if (contentModel.UpdateContent(contentID, content) == -1) {
                session.setAttribute(Static.error, contentModel.GetMessage());
            } else {
                session.setAttribute(Static.confirm, contentModel.GetMessage());
            }

            response.sendRedirect("content");
        } catch (Exception ex) {
        }
    }

    public void Show(HttpServletRequest request, HttpServletResponse response) {
        try {
            HttpSession session = request.getSession(true);

            int contentID = Integer.parseInt(request.getParameter("pages"));
            ContentModel contentModel = new ContentModel();

            HashMap page = contentModel.GetPage(contentID);
            session.setAttribute(Static.page, page);
            getServletContext().getRequestDispatcher("/admin/content/edit.jsp").forward(request, response);
        } catch (Exception ex) {
        }
    }

    public void Index(HttpServletRequest request, HttpServletResponse response) {
        try {
            HttpSession session = request.getSession(true);
            ContentModel contentModel = new ContentModel();
            ArrayList pages = contentModel.GetPages();
            session.setAttribute(Static.pages, pages);
            getServletContext().getRequestDispatcher("/admin/content/index.jsp").forward(request, response);
        } catch (Exception ex) {
        }
    }
}
